package com.wl156.activities;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import com.wl156.MyApp;
import com.wl156.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.services.DownloadIntentService;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class Quit extends Activity implements OnClickListener {

	protected static final String TAG = null;

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_quit);

		findViewById(R.id.quit).setOnClickListener(this);
		findViewById(R.id.logout).setOnClickListener(this);
		findViewById(R.id.update).setOnClickListener(this);

		mWebServiceClient = new WebServiceClient(Quit.this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.quit:
			finish();
			break;
		case R.id.logout:
			new LogoutTask(this).execute();
			break;
		case R.id.update:
			new AsyncTask<Void, Void, Object>() {

				protected void onPostExecute(Object result) {
					try {
						if (result instanceof Exception) {
							Exception e = (Exception) result;

							Toast.makeText(Quit.this, "网络异常，请稍后再试",
									Toast.LENGTH_SHORT).show();
						} else {
							SoapObject so = (SoapObject) result;
							String version = so.getProperty(0).toString();
							String appVersion = Quit.this
									.getString(R.string.version);
							final String url = so.getPropertyAsString(1);

							Log.d(TAG, "version: " + version + ", appVersion: "
									+ appVersion + ", url: " + url);
							if (Float.parseFloat(version) > Float
									.parseFloat(appVersion)) {

								// 下载新版
								AlertDialog.Builder builder = new Builder(
										Quit.this);
								builder.setTitle("有新版本");
								builder.setMessage("当前最新版本为：" + version
										+ ", 是否升级？");
								builder.setPositiveButton("是",
										new DialogInterface.OnClickListener() {

											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {

												Toast.makeText(Quit.this,
														"有新的版本，正在下载中",
														Toast.LENGTH_LONG)
														.show();
												Intent i = new Intent(
														Quit.this,
														DownloadIntentService.class);
												i.putExtra("url", url);
												Quit.this.startService(i);

												i = new Intent(Quit.this,
														MainTab.class);
												startActivity(i);
												finish();
											}
										});
								builder.setNegativeButton("否", null);
								builder.show();
							} else {
								Toast.makeText(Quit.this, "没有新版本",
										Toast.LENGTH_SHORT).show();
							}
						}
					} catch (Exception e) {
						Intent i = new Intent(Quit.this, MainTab.class);
						startActivity(i);
						finish();
					}
				};

				@Override
				protected Object doInBackground(Void... params) {
					try {
						return mWebServiceClient
								.common_AutoUpdate(mCertificate);
					} catch (IOException e) {
						e.printStackTrace();
						return e;
					} catch (XmlPullParserException e) {
						e.printStackTrace();
						return e;
					}
				}
			}.execute();
			break;
		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		MainTab.tabHost.setCurrentTabByTag("home");
	}
}
